package bTree.LeetCode;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class 钥匙和房间 {
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        int size = rooms.size();
        int cnt=1;
        boolean []visited=new boolean[size];
        Queue<List<Integer>>queue=new LinkedList<>();
        queue.add(rooms.get(0));
        visited[0]=true;
        while (!queue.isEmpty()){
            int queueSize = queue.size();
            for (int i = 0; i < queueSize; i++) {
                List<Integer> peek = queue.peek();
                if (peek.isEmpty()){
                    queue.poll();
                    continue;
                }
                for (Integer integer : peek) {
                    if (!visited[integer]){
                        visited[integer]=true;
                        cnt++;
                        queue.add(rooms.get(integer));
                    }
                }
                queue.poll();
            }
        }
        return cnt==size;
    }
}